/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.util.List;
import pojo.User;

/**
 *
 * @author 4pril
 */
public class UserDAO extends MyQuery{
    
    @Override
    protected Class getPOJOClass() {
        return this.getClass();
    }
            
    public static User getUser(int user_id){
        return (User) getObject( User.class, user_id);
    }

    public static boolean addUser(User user){
        return addObject(user);
    }
    
    public static List<User> getListUser(String hql){
        return (List<User>) (List<?>) getList(hql);
    }
    
    public static boolean login(User user){       
        
        //get list userMyQuery
        List<User> ds = getListUser("from User");
        
        //duyet tim thang trung
        for (int i = 0; i < ds.size(); i++) {
            if(ds.get(i).getUsername().equals(user.getUsername()) && ds.get(i).getPassword().equals(user.getPassword())){       
                return true;
            }
        }           
        
        return false;
    }   
    
    public static User getLoginUser(User user){
        User ur = null;
        
        //get list userMyQuery
        List<User> ds = getListUser("from User");
        
        //duyet tim thang trung
        for (int i = 0; i < ds.size(); i++) {
            if(ds.get(i).getUsername().equals(user.getUsername()) && ds.get(i).getPassword().equals(user.getPassword())){       
                ur = ds.get(i);
            }
        }           
        
        return ur;
    }
    
    public static boolean checkExists(User user){
        List<User> ds = getListUser("from User");
        
        //duyet tim thang trung
        for (int i = 0; i < ds.size(); i++) {
            if(ds.get(i).getUsername().equals(user.getUsername())){       
                return true;
            }
        }           
        
        return false;
    }
}
